package com.huanxin;

import com.easemob.im.server.EMProperties;
import com.easemob.im.server.EMService;
import com.easemob.im.server.model.EMTextMessage;

import java.util.HashSet;
import java.util.Set;

public class HuanxinTemplate {

    private EMService emService;

    public HuanxinTemplate(HuanxinProperties properties) {
        EMProperties emProperties = EMProperties.builder()
                .setAppkey(properties.getAppkey())
                .setClientId(properties.getClientId())
                .setClientSecret(properties.getClientSecret())
                .build();

        this.emService = new EMService(emProperties);
    }

    /**
     * 创建环信用户
     */
    public void createUser(String username,String password){
        emService.user().create(username,password).block();
    }
    /**
     * 创建好友关系
     */
    public void addFriend(String username1,String username2){
        emService.contact().add(username1,username2).block();
    }

    /**
     * 删除好友关系
     */
    public void deleteFriend(String username1,String username2){
        emService.contact().remove(username1,username2).block();
    }
    /**
     * 发送消息
     */
    public void sendMsg(String username,String msg){
        Set<String> set = new HashSet<>();
        set.add(username);
        emService.message().send("admin","users",set,new EMTextMessage().text(msg),null).block();


    }



}
